$(document).ready(function () {

	//点击登录按钮
	login_click();
	function login_click() {
		var login_form = $('#login_form');
		var login_tip = $('#login_tip');
		$('#login').on('click',function () {
			var username = login_form.find('input[name="username"]').val();
			var passwd = login_form.find('input[name="passwd"]').val();

			if( data_tip(login_form) ){
				post_login_data();//提交登录参数
			}else {
				if( !username ){
					modal_fix('login_tip','请输入账号');
					return;
				}
				if( !passwd ){
					modal_fix('login_tip','请输入密码');
					return;
				}
			}
		});
	}

	//提交登录参数
	function post_login_data() {
		var login_form = $('#login_form');
		$.ajax({
			url: "/user/checklogin",
			dataType: "json",
			type: "post",
			data: {
				username: login_form.find('input[name="username"]').val(),
				passwd: login_form.find('input[name="passwd"]').val()
			},
			success: function (data) {
				if( data.code == '1' ){
					window.location = '/index/index'
				}else {
					modal_fix('login_tip',data.msg);
				}
			},
			error: function (err) {
				console.log(err);
			}
		})
	}

	//参数缺失提示
	function data_tip(form) {
		var username = form.find('input[name="username"]').val();
		var passwd = form.find('input[name="passwd"]').val();
		if( username && passwd ){
			return true
		}else {
			return false;
		}
	}

	//modal
	function modal_fix(modal_id,text) {
		$('#' + modal_id).find('.modal-body').text(text);
		$('#' + modal_id).modal({keyboard:false})
	}
});